package com.avertymaroc.beans;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.avertymaroc.users.SondagesRapides;


public class SondageRapide implements Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String KEY_ID 		= "id";
	public static final String KEY_TITRE 	= "titre";
	public static final String KEY_SPONSO 	= "sponso";
	public static final String KEY_MULTI 	= "multi";
	public static final String KEY_POINTS 	= "point";
	public static final String KEY_VOTES 	= "votes";
	public static final String KEY_DUREE 	= "duree";
	public static final String KEY_CURRENT 	= "current";
	public static final String KEY_USER 	= "user";
	public static final String KEY_SUIVI 	= "suivi";
	public static final String KEY_SHOW_RES = "show_resultats";
	public static final String KEY_OPTIONS 	= "options";
	
	
	private Integer 	id;
	private String 		titre;
	private Integer		points;
	private Integer		votes;
	private Date		duree;
	private Date		current;
	private User		user;
	private boolean 	suivi;
	private boolean 	sponso;
	private boolean 	multi;
	private boolean		show_resultats;
	private Option[]	options;
	
	
	
	
	public SondageRapide() {}
	public SondageRapide(JSONObject s) throws JSONException, ParseException {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");

		id 				= s.getInt(KEY_ID);
		titre 			= s.getString(KEY_TITRE);
		sponso			= (s.getInt(KEY_SPONSO) == 1);
		multi			= ( !s.getString(KEY_MULTI).equals("radio" ));
		//points			= s.getInt(KEY_POINTS);
		votes			= s.getInt(KEY_VOTES);
		
		duree 			= sdf.parse(s.getString(KEY_DUREE));
		current 		= sdf.parse(s.getString(KEY_CURRENT));
		suivi			= s.getBoolean(KEY_SUIVI);
		show_resultats 	= s.getBoolean(KEY_SHOW_RES);
		
		setUser(s.getJSONObject(KEY_USER));
		setOptions(s.getJSONArray(KEY_OPTIONS));
			

		
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitre() {
		return titre;
	}
	public void setTitre(String titre) {
		this.titre = titre;
	}
	public boolean isSponso() {
		return sponso;
	}
	public void setSponso(boolean sponso) {
		this.sponso = sponso;
	}
	public boolean isMulti() {
		return multi;
	}
	public void setMulti(boolean multi) {
		this.multi = multi;
	}
	public Integer getPoints() {
		return points;
	}
	public void setPoints(Integer points) {
		this.points = points;
	}
	public Integer getVotes() {
		return votes;
	}
	public void setVotes(Integer votes) {
		this.votes = votes;
	}
	public Date getDuree() {
		return duree;
	}
	public void setDuree(Date duree) {
		this.duree = duree;
	}
	public Date getCurrent() {
		return current;
	}
	public void setCurrent(Date current) {
		this.current = current;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public boolean isSuivi() {
		return suivi;
	}
	public void setSuivi(boolean suivi) {
		this.suivi = suivi;
	}
	public boolean isShow_resultats() {
		return show_resultats;
	}
	public void setShow_resultats(boolean show_resultats) {
		this.show_resultats = show_resultats;
	}
	public Option[] getOptions() {
		return options;
	}
	public void setOption(Option[] options) {
		this.options = options;
	}
	
	
	public void setUser(JSONObject u) throws JSONException{
		
		user = new User();
		user.setPrenom(u.getString(User.KEY_PRENOM));
		user.setImg(u.getString(User.KEY_IMAGE));	
		user.setVille(u.getInt(User.KEY_VILLE) - 1);
		
	}
	public void setOptions(JSONArray opts ) throws JSONException{
		
		options = new Option[opts.length()];
		for(int i=0 ; i<opts.length() ;i++){
			JSONObject obj = opts.getJSONObject(i);
			int id ;
			Double percent;
			String titre;
			id 		= obj.getInt(Option.KEY_ID);
			titre	= obj.getString(Option.KEY_TITRE);
			percent	= obj.getDouble(Option.KEY_PERCENT) ;
			
			options[i] = new Option( id , titre , percent );
			
		}
		
	}
	
	public Map<String, Integer> getDate(){
		
		 Map<String, Integer> duree_ = null ;
			if(duree.getTime() > current.getTime()){
				int j , h , m , s;
				duree_ = new HashMap<String, Integer>();
				
				Long def = duree.getTime() - current.getTime();
				j = (int) (def / (1000*60*60*24));
				def -= j*1000*60*60*24;
				h = (int) (def / (1000*60*60));
				def -= h*1000*60*60;
				m = (int) (def / (1000*60));
				def -= m*1000*60;
				s = (int) (def / (1000));
				
				duree_.put(SondagesRapides.KEY_JOURS , j);
				duree_.put(SondagesRapides.KEY_HOURS , h);
				duree_.put(SondagesRapides.KEY_MIN , m);
				duree_.put(SondagesRapides.KEY_SEC , s);
			}
			
			return duree_;
		
	}
	
}
